
compiler_plugin "FS.Plugins.Ext.DateRelative"
{
    desc            =   "Allows representation of relative dates such as 2nd monday in january";
    examples        =   [ '3rd Monday in January',  '2nd Monday in February',  'Last Monday in May',  '1st Monday in September'  ];
    type            =   "expr";
    precedence      =   1;  
    grammar_match   =   "$day:@starttoken.tvaluestring $dayend:( st | nd | rd | th )? $dayofweek:@tokenmap1.value $sep:( in | of ) $month:@tokenmap2.value";
	start_tokens	=   [   "$NumberToken", "first", "First", "second", "Second", "third", "Third", "fourth", "Fourth", "last", "Last" ];
    tokenmap1	    =   {
                            "sunday" : 0,  "monday" : 1, "tuesday" : 2, "wednesday" : 3, "thursday" : 4, "friday" : 5, "saturday" : 6, 
                            "Sunday" : 0,  "Monday" : 1, "Tuesday" : 2, "Wednesday" : 3, "Thursday" : 4, "Friday" : 5, "Saturday" : 6, 
                            "sun"    : 0,  "mon"    : 1, "tues"    : 2, "wed"       : 3, "thurs"    : 4, "fri"    : 5, "sat"      : 6, 
                            "Sun"    : 0,  "Mon"    : 1, "Tues"    : 2, "Wed"       : 3, "Thurs"    : 4, "Fri"    : 5, "Sat"      : 6
                        };
	tokenmap2	    =   {  
                            "jan"        : 1,  "feb"      : 2, "mar"   : 3,    "apr"   : 4,  "may" : 5,      "jun"  : 6,     "jul"  : 7,     "aug"    : 8,   "sep"       : 9,    "oct"     : 10,  "nov"      : 11, "dec"      : 12,             
                            "Jan"        : 1,  "Feb"      : 2, "Mar"   : 3,    "Apr"   : 4,  "May" : 5,      "Jun"  : 6,     "Jul"  : 7,     "Aug"    : 8,   "Sep"       : 9,    "Oct"     : 10,  "Nov"      : 11, "Dec"      : 12,
                            "january"    : 1,  "february" : 2, "march" : 3,    "april" : 4,  "may" : 5,      "june" : 6,     "july" : 7,     "august" : 8,   "september" : 9,    "october" : 10,  "november" : 11, "december" : 12,
                            "January"    : 1,  "February" : 2, "March" : 3,    "April" : 4,  "May" : 5,      "June" : 6,     "July" : 7,     "August" : 8,   "September" : 9,    "October" : 10,  "November" : 11, "December" : 12 
                        };                        
    build           =   function(args)    
                        {       
                            var day = args["dayValue"]; 
							var dayofweek = args["dayofweekValue"];
							var month = args["monthValue"];
                            var token = args["day"];
                            return sys.compiler.toRelativeDate(day, dayofweek, month, token)
                        }
}